package com.whjk.safework.controller;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.core.row.Row;
import com.whjk.safework.entity.ResponseData;
import com.whjk.safework.service.IWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping(value = "/score")
public class Score {

    @Autowired
    private IWorkerService workerService;

    @RequestMapping(value="/doWorkerScore",method = RequestMethod.GET)
    public ResponseData doWorkerScore(@RequestParam(value = "month", defaultValue = "") String month,
                                      @RequestParam(value = "id", defaultValue = "") Integer workerId) {
        ResponseData   responseData      =  new ResponseData();
        try {

            String        orgTargetSql       =  "select DISTINCT(t_worker_target.target_id) as targetId,target_str,kind_id,t_dic.name as kName from t_worker_target join t_dic on t_worker_target.kind_id=t_dic.id  where worker_id = ? and DATE_FORMAT(start_time,'%Y')=?";
            List<Row>     orgTargetRows      =  Db.selectListBySql(orgTargetSql,workerId,month.substring(0,4));

            for (Row row_1:orgTargetRows) {
                String                  orgSafeTargetSql   =  "select DISTINCT(t_worker_target.safetarget_id) as safetargetId,target_str,dutyinfo_str,plan_str,check_pc,check_str from t_worker_target  where worker_id = ? and DATE_FORMAT(start_time,'%Y')=? and target_id=?";
                List<Row>               orgSafeTargetRows  =   Db.selectListBySql(orgSafeTargetSql,workerId,month.substring(0,4),row_1.getInt("targetId"));
                row_1.set("children",orgSafeTargetRows);
            }

            List<Row>  allList          =   new ArrayList<>(); // 员工任务记录
            List<Row>  rowList_week     =   workerService.getWorkerWeekRecord(workerId,month); // 获取 周任务记录
            List<Row>  rowList_month    =   workerService.getWorkerMonthRecord(workerId,month);// 获取 月任务记录
            List<Row>  rowList_quarter  =   workerService.getWorkerQuarterRecord(workerId,month);// 获取 季度任务记录
            List<Row>  rowList_year     =   workerService.getWorkerYearRecord(workerId,month.substring(0,4));// 获取 年任务记录

            allList.addAll(rowList_week);
            allList.addAll(rowList_month);
            allList.addAll(rowList_quarter);
            allList.addAll(rowList_year);

            for (Row row_1_new:orgTargetRows) {
                List<Row>  row_2_list    =  (List<Row>)row_1_new.get("children");
                for (Row row_2_new:row_2_list) {
                    List<Row> children  = new ArrayList<>();
                    for(Row r:allList) {
                        int row2_safetargetId   =    row_2_new.getInt("safetargetId");
                        int r_safetargetId      =    r.getInt("safetarget_id");
                        if (row2_safetargetId == r_safetargetId){
                            children.add(r);
                        }
                    }
                    row_2_new.set("children",children);
                }
                row_1_new.set("children",row_2_list);
            }

            BigDecimal  E_leader            = new BigDecimal(1);
            BigDecimal  E_my                = new BigDecimal(1);
            BigDecimal  result_leader_score = new BigDecimal(BigInteger.ZERO);
            BigDecimal  result_my_score     = new BigDecimal(BigInteger.ZERO);

            //orgTargetRows  企业履职指标
            for(Row jsr:orgTargetRows){
                List<Row> safeRowList     = (List<Row>)jsr.get("children");//岗位履职目标指标
                BigDecimal leader_score_1 = new BigDecimal(BigInteger.ZERO);
                BigDecimal my_score_1     = new BigDecimal(BigInteger.ZERO);
                int orgTargetNum          = 0;

                for (Row saferow:safeRowList) {
                    List<Row> scoreRowList = (List<Row>)saferow.get("children"); //岗位履职下的任务
                    int         num             = 0;
                    BigDecimal  leader_score    = new BigDecimal(BigInteger.ZERO);
                    BigDecimal  my_score        = new BigDecimal(BigInteger.ZERO);

                    Integer[]  ids = new Integer[scoreRowList.size()];

                    for (Row scorerow:scoreRowList) {
                        ids[num] = scorerow.getInt("id");
                        BigDecimal aWeight      =   scorerow.getBigDecimal("aweight");//企业目标指标
                        BigDecimal bWeight      =   scorerow.getBigDecimal("bweight");//部门目标指标
                        BigDecimal a            =   new BigDecimal(10);
                        BigDecimal b            =   new BigDecimal(10);

                        if(scorerow.getInt("kind_id")==2){ // 过程性指标

                            LocalDateTime endTime   =    scorerow.getLocalDateTime("end_time");
                            // 结束时间在当前时间后  未逾期过程
                            if(endTime.isAfter(LocalDateTime.now())){
//                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
//                                    a            =  new BigDecimal(10) ;
//                                }
//                                else   a         =  scorerow.getBigDecimal("check_result");
//                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
//                                    b            =  new BigDecimal(10) ;
//                                }
//                                else   b         =  scorerow.getBigDecimal("my_check_result");


                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                    b       =  new BigDecimal(0) ;
                                }
                                else   b         =  scorerow.getBigDecimal("my_check_result");

                                if(b.compareTo(BigDecimal.ZERO) > 0){ //自评了
                                    if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                        a            =  new BigDecimal(10) ;
                                    }
                                    else   a         =  scorerow.getBigDecimal("check_result");
                                }
                                else {
                                    if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                        a            =  new BigDecimal(0) ;
                                    }
                                    else   a         =  scorerow.getBigDecimal("check_result");
                                }


                            }
                            else { // 结束时间在当前时间前面  已经逾期过程
                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                    a            =  new BigDecimal("0") ;
                                }
                                else   a         =  scorerow.getBigDecimal("check_result");
                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                    b            =  new BigDecimal("0") ;
                                }
                                else   b         =  scorerow.getBigDecimal("my_check_result");
                            }
                            BigDecimal leader_temp_score   =  aWeight .multiply(bWeight).multiply(a).multiply(new BigDecimal(10));
                            BigDecimal my_temp_score       =  aWeight .multiply(bWeight).multiply(b).multiply(new BigDecimal(10));
                                       leader_score        =  leader_score.add(leader_temp_score);
                                       my_score            =  my_score.add(my_temp_score);
                        }

                        // 结果性指标
                        if(scorerow.getInt("kind_id")==1&& a.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                            E_leader=  new BigDecimal(BigInteger.ZERO);
                        }
                        if(scorerow.getInt("kind_id")==1&& b.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                            E_my=  new BigDecimal(BigInteger.ZERO);
                        }
                        num++;
                    }

                    if(num>=1) {
                        leader_score    = leader_score.divide(new BigDecimal(num),2);
                        my_score        = my_score.divide(new BigDecimal(num),2);
                    }
                    saferow.set("leader_score",leader_score);
                    saferow.set("my_score",my_score);
                    saferow.set("childrenIds",ids);

                    leader_score_1 =  leader_score_1.add(leader_score);
                    my_score_1     =  my_score_1.add(my_score);
                    orgTargetNum++;
                }
                if(orgTargetNum>=1){
                    leader_score_1 =  leader_score_1.divide(new BigDecimal(orgTargetNum),2);
                    my_score_1     =  my_score_1.divide(new BigDecimal(orgTargetNum),2);
                }
                jsr.set("leader_score",leader_score_1);
                jsr.set("my_score",my_score_1);
                result_leader_score     =    result_leader_score.add(leader_score_1);
                result_my_score         =    result_my_score.add(my_score_1);
            }
            result_leader_score         =    result_leader_score.multiply(E_leader) ;
            result_my_score             =    result_my_score.multiply(E_my) ;

            responseData.setMsg(allList.size()+"条");
            Row  result = new Row();
            result.set("leadScore",result_leader_score);
            result.set("myScore",result_my_score);
            result.set("E",E_leader);
            result.set("data",orgTargetRows);
            responseData.setCode(200);
            responseData.setData(result);
        }catch (Exception e){
            responseData.setCode(201);
            responseData.setData(e.getMessage());
        }
        return responseData;
    }



    @RequestMapping(value="/doYearScoreByworkId",method = RequestMethod.GET)
    public ResponseData doYearScoreByworkId(@RequestParam(value = "year", defaultValue = "0") Integer year,
                                      @RequestParam(value = "id", defaultValue = "") Integer workerId) {
        ResponseData   responseData      =  new ResponseData();
        try {
            String      orgTargetSql     = "select DISTINCT(t_worker_target.target_id) as targetId,target_str,kind_id,t_dic.name as kName from t_worker_target join t_dic on t_worker_target.kind_id=t_dic.id  where worker_id = ? and year=?";
            List<Row>   orgTargetRows    = Db.selectListBySql(orgTargetSql, workerId, year);
            //System.out.println(orgTargetRows);
            for (Row row_1 : orgTargetRows) {
                String      orgSafeTargetSql    = "select DISTINCT(t_worker_target.safetarget_id) as safetargetId,target_str,dutyinfo_str,plan_str,check_pc,check_str from t_worker_target  where worker_id = ? and year=? and target_id=?";
                List<Row>   orgSafeTargetRows   =  Db.selectListBySql(orgSafeTargetSql, workerId, year, row_1.getInt("targetId"));
                row_1.set("children", orgSafeTargetRows);
            }
            List<Row>  allList     =   workerService.getWorkerAllYearRecord(workerId,year+"");

            for (Row row_1_new:orgTargetRows) {
                List<Row>  row_2_list    =  (List<Row>)row_1_new.get("children");
                for (Row row_2_new:row_2_list) {
                    List<Row> children  = new ArrayList<>();
                    for(Row r:allList) {
                        int row2_safetargetId  =    row_2_new.getInt("safetargetId");
                        int r_safetargetId     =    r.getInt("safetarget_id");

                        if (row2_safetargetId == r_safetargetId){
                            children.add(r);
                        }
                    }
                    row_2_new.set("children",children);
                }
                row_1_new.set("children",row_2_list);
            }

            BigDecimal  E_leader            = new BigDecimal(1);
            BigDecimal  E_my                = new BigDecimal(1);
            BigDecimal  result_leader_score = new BigDecimal(BigInteger.ZERO);
            BigDecimal  result_my_score     = new BigDecimal(BigInteger.ZERO);

            for(Row jsr:orgTargetRows){
                List<Row> safeRowList     = (List<Row>)jsr.get("children");
                BigDecimal leader_score_1 = new BigDecimal(BigInteger.ZERO);
                BigDecimal my_score_1     = new BigDecimal(BigInteger.ZERO);
                int orgTargetNum          = 0;

                for (Row saferow:safeRowList) {
                    List<Row> scoreRowList = (List<Row>)saferow.get("children");
                    int         num             = 0;
                    BigDecimal  leader_score    = new BigDecimal(BigInteger.ZERO);
                    BigDecimal  my_score        = new BigDecimal(BigInteger.ZERO);

                    Integer[]  ids = new Integer[scoreRowList.size()];

                    for (Row scorerow:scoreRowList) {
                        ids[num] = scorerow.getInt("id");
                        BigDecimal aWeight      =   scorerow.getBigDecimal("aweight");
                        BigDecimal bWeight      =   scorerow.getBigDecimal("bweight");
                        BigDecimal a            =   new BigDecimal(10);
                        BigDecimal b            =   new BigDecimal(10);

                        if(scorerow.getInt("kind_id")==2){
                            LocalDateTime endTime   =    scorerow.getLocalDateTime("end_time");
                            // 结束时间在当前时间后  未逾期过程
                            if(endTime.isAfter(LocalDateTime.now())){
//                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
//                                    a            =  new BigDecimal(10) ;
//                                }
//                                else   a         =  scorerow.getBigDecimal("check_result");
//                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
//                                    b            =  new BigDecimal(10) ;
//                                }
//                                else   b         =  scorerow.getBigDecimal("my_check_result");

                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                    b       =  new BigDecimal(0) ;
                                }
                                else   b         =  scorerow.getBigDecimal("my_check_result");

                                if(b.compareTo(BigDecimal.ZERO) > 0){ //自评了
                                    if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                        a            =  new BigDecimal(10) ;
                                    }
                                    else   a         =  scorerow.getBigDecimal("check_result");
                                }
                                else {
                                    if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                        a            =  new BigDecimal(0) ;
                                    }
                                    else   a         =  scorerow.getBigDecimal("check_result");
                                }


                            }
                            else { // 结束时间在当前时间前面  已经逾期过程
                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                    a            =  new BigDecimal("0") ;
                                }
                                else   a         =  scorerow.getBigDecimal("check_result");
                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                    b            =  new BigDecimal("0") ;
                                }
                                else   b         =  scorerow.getBigDecimal("my_check_result");
                            }
                            BigDecimal leader_temp_score   =  aWeight .multiply(bWeight).multiply(a).multiply(new BigDecimal(10));
                            BigDecimal my_temp_score       =  aWeight .multiply(bWeight).multiply(b).multiply(new BigDecimal(10));
                            leader_score        =  leader_score.add(leader_temp_score);
                            my_score            =  my_score.add(my_temp_score);
                        }
                        if(scorerow.getInt("kind_id")==1&& a.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                            E_leader=  new BigDecimal(BigInteger.ZERO);
                        }
                        if(scorerow.getInt("kind_id")==1&& b.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                            E_my=  new BigDecimal(BigInteger.ZERO);
                        }
                        num++;
                    }
                    if(num>=1) {
                        leader_score    = leader_score.divide(new BigDecimal(num),2);
                        my_score        = my_score.divide(new BigDecimal(num),2);
                    }
                    saferow.set("leader_score",leader_score);
                    saferow.set("my_score",my_score);
                    saferow.set("childrenIds",ids);
                    leader_score_1 =  leader_score_1.add(leader_score);
                    my_score_1     =  my_score_1.add(my_score);
                    orgTargetNum++;
                }
                if(orgTargetNum>=1){
                    leader_score_1 =  leader_score_1.divide(new BigDecimal(orgTargetNum),2);
                    my_score_1     =  my_score_1.divide(new BigDecimal(orgTargetNum),2);
                }
                jsr.set("leader_score",leader_score_1);
                jsr.set("my_score",my_score_1);
                result_leader_score     =    result_leader_score.add(leader_score_1);
                result_my_score         =    result_my_score.add(my_score_1);
            }
            result_leader_score         =    result_leader_score.multiply(E_leader) ;
            result_my_score             =    result_my_score.multiply(E_my) ;

            responseData.setMsg(allList.size()+"条");

            BigDecimal temp_leader_score = result_leader_score.multiply(new BigDecimal("0.4"));
            BigDecimal temp_my_score     = result_my_score.multiply(new BigDecimal("0.6"));

            Row     result = new Row();
                    result.set("leadScore",result_leader_score);
                    result.set("myScore",result_my_score);
                    result.set("averageScore", temp_leader_score.add(temp_my_score)); //平均分
                    result.set("E",E_leader);

          //  workerService.addUpdateWorkYearRecord(workerId,year,result_leader_score,result_my_score,temp_leader_score.add(temp_my_score));

            result.set("data",orgTargetRows);
            responseData.setCode(200);
            responseData.setData(result);
        }catch (Exception e){
            responseData.setCode(201);
           // e.printStackTrace();
            responseData.setData(e.getMessage());
        }
        return responseData;
    }



    @RequestMapping(value="/doMonthScoreByworkId",method = RequestMethod.GET)
    public ResponseData doMonthScoreByworkId(@RequestParam(value = "month", defaultValue = "") String month, @RequestParam(value = "id", defaultValue = "") Integer workerId) {
        ResponseData   responseData      =  new ResponseData();
        try {
            String        orgTargetSql       =  "select DISTINCT(t_worker_target.target_id) as targetId,target_str,kind_id,t_dic.name as kName from t_worker_target join t_dic on t_worker_target.kind_id=t_dic.id  where worker_id = ? and DATE_FORMAT(start_time,'%Y')=?";
            List<Row>     orgTargetRows      =  Db.selectListBySql(orgTargetSql,workerId,month.substring(0,4));

            for (Row row_1:orgTargetRows) {
                String                  orgSafeTargetSql   =  "select DISTINCT(t_worker_target.safetarget_id) as safetargetId,target_str,dutyinfo_str,plan_str,check_pc,check_str from t_worker_target  where worker_id = ? and DATE_FORMAT(start_time,'%Y')=? and target_id=?";
                List<Row>               orgSafeTargetRows  =   Db.selectListBySql(orgSafeTargetSql,workerId,month.substring(0,4),row_1.getInt("targetId"));
                row_1.set("children",orgSafeTargetRows);
            }

            List<Row>  allList          =   new ArrayList<>(); // 员工任务记录
            List<Row>  rowList_week     =   workerService.getWorkerWeekRecord(workerId,month); // 获取 周任务记录
            List<Row>  rowList_month    =   workerService.getWorkerMonthRecord(workerId,month);// 获取 月任务记录
            List<Row>  rowList_quarter  =   workerService.getWorkerQuarterRecord(workerId,month);// 获取 季度任务记录
            List<Row>  rowList_year     =   workerService.getWorkerYearRecord(workerId,month.substring(0,4));// 获取 年任务记录

            allList.addAll(rowList_week);
            allList.addAll(rowList_month);
            allList.addAll(rowList_quarter);
            allList.addAll(rowList_year);

            for (Row row_1_new:orgTargetRows) {
                List<Row>  row_2_list    =  (List<Row>)row_1_new.get("children");
                for (Row row_2_new:row_2_list) {
                    List<Row> children  = new ArrayList<>();
                    for(Row r:allList) {
                        int row2_safetargetId  =    row_2_new.getInt("safetargetId");
                        int r_safetargetId     =    r.getInt("safetarget_id");
                        if (row2_safetargetId == r_safetargetId){
                            children.add(r);
                        }
                    }
                    row_2_new.set("children",children);
                }
                row_1_new.set("children",row_2_list);
            }

            BigDecimal  E_leader            = new BigDecimal(1);
            BigDecimal  E_my                = new BigDecimal(1);
            BigDecimal  result_leader_score = new BigDecimal(BigInteger.ZERO);
            BigDecimal  result_my_score     = new BigDecimal(BigInteger.ZERO);

            for(Row jsr:orgTargetRows){
                List<Row> safeRowList     = (List<Row>)jsr.get("children");
                BigDecimal leader_score_1 = new BigDecimal(BigInteger.ZERO);
                BigDecimal my_score_1     = new BigDecimal(BigInteger.ZERO);
                int orgTargetNum          = 0;

                for (Row saferow:safeRowList) {
                    List<Row> scoreRowList = (List<Row>)saferow.get("children");
                    int         num             = 0;
                    BigDecimal  leader_score    = new BigDecimal(BigInteger.ZERO);
                    BigDecimal  my_score        = new BigDecimal(BigInteger.ZERO);

                    Integer[]  ids = new Integer[scoreRowList.size()];

                    for (Row scorerow:scoreRowList) {
                        ids[num] = scorerow.getInt("id");
                        BigDecimal aWeight      =   scorerow.getBigDecimal("aweight");
                        BigDecimal bWeight      =   scorerow.getBigDecimal("bweight");
                        BigDecimal a            =   new BigDecimal(10);
                        BigDecimal b            =   new BigDecimal(10);

                        if(scorerow.getInt("kind_id")==2){
                            LocalDateTime endTime   =    scorerow.getLocalDateTime("end_time");
                            // 结束时间在当前时间后  未逾期过程
                            if(endTime.isAfter(LocalDateTime.now())){
                               /* if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                    a            =  new BigDecimal(10) ;
                                }
                                else   a         =  scorerow.getBigDecimal("check_result");
                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                    b            =  new BigDecimal(10) ;
                                }
                                else   b         =  scorerow.getBigDecimal("my_check_result");*/

                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                    b       =  new BigDecimal(0) ;
                                }
                                else   b         =  scorerow.getBigDecimal("my_check_result");

                                if(b.compareTo(BigDecimal.ZERO) > 0){ //自评了
                                    if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                        a            =  new BigDecimal(10) ;
                                    }
                                    else   a         =  scorerow.getBigDecimal("check_result");
                                }
                                else {
                                    if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                        a            =  new BigDecimal(0) ;
                                    }
                                    else   a         =  scorerow.getBigDecimal("check_result");
                                }


                            }
                            else { // 结束时间在当前时间前面  已经逾期过程
                                if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                    a            =  new BigDecimal("0") ;
                                }
                                else   a         =  scorerow.getBigDecimal("check_result");
                                if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                    b            =  new BigDecimal("0") ;
                                }
                                else   b         =  scorerow.getBigDecimal("my_check_result");
                            }
                            BigDecimal leader_temp_score   =  aWeight .multiply(bWeight).multiply(a).multiply(new BigDecimal(10));
                            BigDecimal my_temp_score       =  aWeight .multiply(bWeight).multiply(b).multiply(new BigDecimal(10));
                            leader_score        =  leader_score.add(leader_temp_score);
                            my_score            =  my_score.add(my_temp_score);
                        }
                        if(scorerow.getInt("kind_id")==1&& a.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                            E_leader=  new BigDecimal(BigInteger.ZERO);
                        }
                        if(scorerow.getInt("kind_id")==1&& b.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                            E_my=  new BigDecimal(BigInteger.ZERO);
                        }
                        num++;
                    }
                    if(num>=1) {
                        leader_score    = leader_score.divide(new BigDecimal(num),2);
                        my_score        = my_score.divide(new BigDecimal(num),2);
                    }
                    saferow.set("leader_score",leader_score);
                    saferow.set("my_score",my_score);
                    saferow.set("childrenIds",ids);
                    leader_score_1 =  leader_score_1.add(leader_score);
                    my_score_1     =  my_score_1.add(my_score);
                    orgTargetNum++;
                }
                if(orgTargetNum>=1){
                    leader_score_1 =  leader_score_1.divide(new BigDecimal(orgTargetNum),2);
                    my_score_1     =  my_score_1.divide(new BigDecimal(orgTargetNum),2);
                }
                jsr.set("leader_score",leader_score_1);
                jsr.set("my_score",my_score_1);
                result_leader_score     =    result_leader_score.add(leader_score_1);
                result_my_score         =    result_my_score.add(my_score_1);
            }
            result_leader_score         =    result_leader_score.multiply(E_leader) ;
            result_my_score             =    result_my_score.multiply(E_my) ;

            responseData.setMsg(allList.size()+"条");

            BigDecimal temp_leader_score = result_leader_score.multiply(new BigDecimal("0.4"));
            BigDecimal temp_my_score     = result_my_score.multiply(new BigDecimal("0.6"));

            Row     result = new Row();
                    result.set("leadScore",result_leader_score);
                    result.set("myScore",result_my_score);
                    result.set("averageScore", temp_leader_score.add(temp_my_score)); //平均分
                    result.set("E",E_leader);

            workerService.addUpdateWorkMonthRecord(workerId,month,result_leader_score,result_my_score,temp_leader_score.add(temp_my_score));

            //result.set("data",orgTargetRows);
            responseData.setCode(200);
            responseData.setData(result);
        }catch (Exception e){
            responseData.setCode(201);
            // e.printStackTrace();
            responseData.setData(e.getMessage());
        }
        return responseData;
    }

    @RequestMapping(value="/doYearWorkerScore",method = RequestMethod.GET)
    public ResponseData doYearWorkerScore(@RequestParam(value = "year", defaultValue = "") String year,@RequestParam(value = "id", defaultValue = "") Integer workerId) {
        ResponseData        responseData   =    new ResponseData();
        try {
            String      orgTargetSql     = "select DISTINCT(t_worker_target.target_id) as targetId,target_str,kind_id,t_dic.name as kName from t_worker_target join t_dic on t_worker_target.kind_id=t_dic.id  where worker_id = ? and year=?";
            List<Row>   orgTargetRows    = Db.selectListBySql(orgTargetSql, workerId, year);

            for (Row row_1 : orgTargetRows) {
                String      orgSafeTargetSql    = "select DISTINCT(t_worker_target.safetarget_id) as safetargetId,target_str,dutyinfo_str,plan_str,check_pc,check_str from t_worker_target  where worker_id = ? and year=? and target_id=?";
                List<Row>   orgSafeTargetRows   =  Db.selectListBySql(orgSafeTargetSql, workerId, year, row_1.getInt("targetId"));
                row_1.set("children", orgSafeTargetRows);
            }
            List<Row>  allList    =   new ArrayList<>();
            List<Row>  rowList_year     =   workerService.getWorkerAllYearRecord(workerId,year);
            allList.addAll(rowList_year);

            for (Row row_1_new:orgTargetRows) {
                List<Row>  row_2_list    =  (List<Row>)row_1_new.get("children");
                for (Row row_2_new:row_2_list) {
                    List<Row> children  = new ArrayList<>();
                    for(Row r:allList) {
                        int row2_safetargetId  =    row_2_new.getInt("safetargetId");
                        int r_safetargetId     =    r.getInt("safetarget_id");

                        if (row2_safetargetId == r_safetargetId){
                            children.add(r);
                        }
                    }
                    row_2_new.set("children",children);
                }
                row_1_new.set("children",row_2_list);
            }

            BigDecimal  E_leader            = new BigDecimal(1);
            BigDecimal  E_my                = new BigDecimal(1);
            BigDecimal  result_leader_score = new BigDecimal(BigInteger.ZERO);
            BigDecimal  result_my_score     = new BigDecimal(BigInteger.ZERO);

            for(Row jsr:orgTargetRows){
                List<Row> safeRowList     = (List<Row>)jsr.get("children");
                BigDecimal leader_score_1 = new BigDecimal(BigInteger.ZERO);
                BigDecimal my_score_1     = new BigDecimal(BigInteger.ZERO);
                int orgTargetNum          = 0;
                for (Row saferow:safeRowList) {
                    List<Row> scoreRowList = (List<Row>)saferow.get("children");
                    int         num             = 0;
                    BigDecimal  leader_score    = new BigDecimal(BigInteger.ZERO);
                    BigDecimal  my_score        = new BigDecimal(BigInteger.ZERO);

                    Integer[]  ids = new Integer[scoreRowList.size()];

                    for (Row scorerow:scoreRowList) {
                        ids[num] = scorerow.getInt("id");
                        BigDecimal aWeight      =   scorerow.getBigDecimal("aweight");
                        BigDecimal bWeight      =   scorerow.getBigDecimal("bweight");
                        BigDecimal a            =   new BigDecimal(10);
                        BigDecimal b            =   new BigDecimal(10);

                        if(scorerow.getInt("kind_id")==2){
                            if(scorerow.getBigDecimal("check_result")==null||scorerow.getInt("check_result")==0){
                                a            =  new BigDecimal(10) ;
                            }
                            else   a            =  scorerow.getBigDecimal("check_result");
                            if(scorerow.getBigDecimal("my_check_result")==null||scorerow.getInt("my_check_result")==0){
                                b            =  new BigDecimal(10) ;
                            }
                            else   b            =  scorerow.getBigDecimal("my_check_result");
                            BigDecimal leader_temp_score   =  aWeight .multiply(bWeight).multiply(a).multiply(new BigDecimal(10));
                            BigDecimal my_temp_score       =  aWeight .multiply(bWeight).multiply(b).multiply(new BigDecimal(10));
                            leader_score        =  leader_score.add(leader_temp_score);
                            my_score            =  my_score.add(my_temp_score);
                        }
                        if(scorerow.getInt("kind_id")==1&& a.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                            E_leader=  new BigDecimal(BigInteger.ZERO);
                        }
                        if(scorerow.getInt("kind_id")==1&& b.compareTo(new BigDecimal(BigInteger.ZERO))==0){
                            E_my=  new BigDecimal(BigInteger.ZERO);
                        }
                        num++;
                    }

                    if(num>=1) {
                        leader_score    = leader_score.divide(new BigDecimal(num),2);
                        my_score        = my_score.divide(new BigDecimal(num),2);
                    }
                    saferow.set("leader_score",leader_score);
                    saferow.set("my_score",my_score);
                    saferow.set("childrenIds",ids);

                    leader_score_1 =  leader_score_1.add(leader_score);
                    my_score_1     =  my_score_1.add(my_score);
                    orgTargetNum++;
                }
                if(orgTargetNum>=1){
                    leader_score_1 =  leader_score_1.divide(new BigDecimal(orgTargetNum),2);
                    my_score_1     =  my_score_1.divide(new BigDecimal(orgTargetNum),2);
                }
                jsr.set("leader_score",leader_score_1);
                jsr.set("my_score",my_score_1);
                result_leader_score     =    result_leader_score.add(leader_score_1);
                result_my_score         =    result_my_score.add(my_score_1);
            }
            result_leader_score         =    result_leader_score.multiply(E_leader) ;
            result_my_score             =    result_my_score.multiply(E_my) ;

            responseData.setMsg(allList.size()+"条");
            Row  result = new Row();
            result.set("leadScore",result_leader_score);
            result.set("myScore",result_my_score);
            result.set("E",E_leader);
            result.set("data",orgTargetRows);

            responseData.setCode(200);
            responseData.setData(result);

        }catch (Exception e){
            responseData.setCode(201);
            responseData.setData(e.getMessage());
        }
        return responseData;
    }




    @RequestMapping(value="/doWorkersMonth",method = RequestMethod.GET)
    public ResponseData doWorkersMonth(@RequestParam(value = "month", defaultValue = "") String month) {
        ResponseData   responseData      =  new ResponseData();
        try {
            List<Row>  workerList   =  Db.selectListBySql("select * from t_worker   where work_status=0 and is_del=0 and user_id IS NOT NULL ");
            if(month.equals("")) {
                month =DateUtil.format(DateUtil.date(),"yyyy-MM");
            }
            responseData.setMsg(month);
           for (Row workerRow:workerList) {
               workerService.refreshWorkersMonthSocre(workerRow.getInt("id"), month);
            }
        }catch (Exception e){
            responseData.setCode(201);
             e.printStackTrace();
            responseData.setData(e.getMessage());
        }
        return responseData;

    }



    @RequestMapping(value="/hello",method = RequestMethod.GET)
    public ResponseData hello(){
        ResponseData        responseData   =    new ResponseData();


                            responseData.setData(DateUtil.format(DateUtil.date(),"yyyy-MM"));
        responseData.setData(ObjectUtil.isEmpty(null));
                            responseData.setMsg("加载成功");
        return  responseData;
    }



}
